home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / test / ICMP / icmp_timexceed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  5.1 KB  |  153 lines

  1. /*
  2.  *  $Id: icmp_timexceed.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  icmp_unreach.c - ICMP_UNREACH Packet assembly tester
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions
  13.  * are met:
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  *
  32.  */
  33.  
  34. #if (HAVE_CONFIG_H)
  35. #include "../../include/config.h"
  36. #endif
  37. #include "../libnet_test.h"
  38.  
  39. int
  40. main(int argc, char **argv)
  41. {
  42.     int sock, c;
  43.     u_char *buf;
  44.     u_long src_ip, dst_ip;
  45.  
  46.     printf("ICMP_TIMEXCEED packet building/writing test\n");
  47.  
  48.     src_ip = 0;
  49.     dst_ip = 0;
  50.     while((c = getopt(argc, argv, "d:s:")) != EOF)
  51.     {
  52.         switch (c)
  53.         {
  54.             case 'd':
  55.                 if (!(dst_ip = name_resolve(optarg, 1)))
  56.                 {
  57.                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
  58.                     exit(1);
  59.                 }
  60.                 break;
  61.             case 's':
  62.                 if (!(src_ip = name_resolve(optarg, 1)))
  63.                 {
  64.                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
  65.                     exit(1);
  66.                 }
  67.                 break;
  68.         }
  69.     }
  70.     if (!src_ip || !dst_ip)
  71.     {
  72.         usage(argv[0]);
  73.         exit(EXIT_FAILURE);
  74.     }
  75.  
  76.     /*
  77.      *  Get our block of memory for the packet.  In this case, we only need
  78.      *  memory for the packet headers.
  79.      */
  80.     buf = malloc(IP_MAXPACKET);
  81.     if (!buf)
  82.     {
  83.         perror("No memory for packet header");
  84.         exit(EXIT_FAILURE);
  85.     }
  86.  
  87.     /*
  88.      *  Open our raw IP socket and set IP_HDRINCL.
  89.      */
  90.     sock = open_raw_sock(IPPROTO_RAW);
  91.     if (sock == -1)
  92.     {
  93.         perror("No socket");
  94.         exit(EXIT_FAILURE);
  95.     }
  96.     
  97.     /*
  98.      *  Build the IP header (shown exploded for commenting).
  99.      */
  100.     build_ip(ICMP_TIMXCEED_H + IP_H,            /* Size of the payload */
  101.             IPTOS_LOWDELAY | IPTOS_THROUGHPUT,  /* IP tos */
  102.             242,                                /* IP ID */
  103.             0,                                  /* Frag stuff */
  104.             48,                                 /* TTL */
  105.             IPPROTO_ICMP,                       /* Transport protocol */
  106.             src_ip,                             /* Source IP */
  107.             dst_ip,                             /* Destination IP */
  108.             NULL,                               /* Pointer to payload (none) */
  109.             0,
  110.             buf);                               /* Packet header memory */
  111.  
  112.     /*
  113.      *  Build the ICMP header.
  114.      */
  115.     build_icmp_unreach(ICMP_TIMXCEED,               /* type */
  116.                 ICMP_TIMXCEED_INTRANS,              /* code */
  117.                 0,
  118.                 IPTOS_LOWDELAY | IPTOS_THROUGHPUT,  /* IP tos */
  119.                 424,                                /* IP ID */
  120.                 0,                                  /* Frag stuff */
  121.                 64,                                 /* TTL */
  122.                 IPPROTO_ICMP,                       /* Transport protocol */
  123.                 dst_ip,                             /* Source IP */
  124.                 src_ip,                             /* Destination IP */
  125.                 NULL,                                /* pointer to payload */
  126.                 0,                                  /* size of payload */
  127.                 buf + IP_H);                        /* packet header memory */
  128.  
  129.     do_checksum(buf, IPPROTO_ICMP, ICMP_TIMXCEED_H);
  130.  
  131.     /*
  132.      *  Write the packet to the network.
  133.      */
  134.     c = write_ip(sock, buf, ICMP_TIMXCEED_H + 2 * IP_H);
  135.     if (c < ICMP_TIMXCEED_H + 2 * IP_H)
  136.     {
  137.         fprintf(stderr, "write_ip\n");
  138.     }
  139.     printf("Completed, wrote %d bytes\n", c);
  140.     free(buf);
  141.  
  142.     return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
  143. }
  144.  
  145.  
  146. void
  147. usage(u_char *name)
  148. {
  149.     fprintf(stderr, "usage: %s -s source_ip -d destination_ip\n ", name);
  150. }
  151.  
  152. /* EOF */
  153.